package com.cgx.tablet.beeper.window;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import com.cgx.tablet.beeper.R;
import com.cgx.tablet.beeper.adapter.SpecialCallAdapter;
import com.cgx.tablet.beeper.bean.CallBean;
import com.cgx.tablet.beeper.bean.SpecialCallBean;
import com.cgx.tablet.beeper.databinding.WindowSpecialCallBinding;
import com.cgx.tablet.beeper.event.RemoveCallbackEvent;
import com.cgx.tablet.beeper.interfaces.GetCallBeanListener;
import com.cgx.tablet.beeper.interfaces.OnItemClickListener;
import com.cgx.tablet.beeper.presenter.SpecialCallPresenter;
import com.cgx.tablet.beeper.utils.ToastUtil;
import com.cgx.tablet.beeper.view.ISpecialCallView;
import com.yatoooon.screenadaptation.ScreenAdapterTools;

import org.greenrobot.eventbus.EventBus;

import java.util.List;


/**
 * Created by Administrator on 2018/9/8 0008.
 * 特定呼叫
 */

public class SpecifiedCallWindow extends PopupWindow implements View.OnClickListener, OnItemClickListener, ISpecialCallView{

    private Context mContext;
    private WindowSpecialCallBinding mBinding;
    private SpecialCallAdapter specialCallAdapter;
    private SpecialCallPresenter specialCallPresenter;
    private GetCallBeanListener getCallBean;
    private OnDismissListener onDismissListener;

    public interface OnDismissListener{
        void onDismiss();
    }

    public void setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    public void setGetCallBeanListener(GetCallBeanListener getCallBean) {
        this.getCallBean = getCallBean;
    }

    public SpecifiedCallWindow(Context context) {
        super(context);
        View view = LayoutInflater.from(context).inflate(R.layout.window_special_call, null);
        mBinding = DataBindingUtil.bind(view);
        mContext = context;
        setContentView(view);
        specialCallPresenter = new SpecialCallPresenter(this);
        ScreenAdapterTools.getInstance().loadView(view);

        mBinding.query.setOnClickListener(this);
        mBinding.closeTv.setOnClickListener(this);
        mBinding.closeIv.setOnClickListener(this);
        view.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    int left = mBinding.specifiedCallLl.getLeft();
                    int right = mBinding.specifiedCallLl.getRight();
                    int top = mBinding.specifiedCallLl.getTop();
                    int bottom = mBinding.specifiedCallLl.getBottom();
                    int x = (int) event.getX();
                    int y = (int) event.getY();
                    if ( x < left || x > right || y > bottom || y < top) {
                        if (onDismissListener != null)
                            onDismissListener.onDismiss();
                        dismiss();
                    }
                }
                v.performClick();
                return true;
            }
        });

        specialCallAdapter = new SpecialCallAdapter(context);
        specialCallAdapter.setOnItemClickListener(this);
        mBinding.specifiedCallLv.setAdapter(specialCallAdapter);

        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        setOutsideTouchable(false);
        setFocusable(true);
        setTouchable(true);
//        this.setAnimationStyle(R.style.top_in_and_out_anim);
        ColorDrawable dw = new ColorDrawable(ContextCompat.getColor(context, R.color.gray_translucent));
        setBackgroundDrawable(dw);

        //sdk > 21 解决 标题栏没有办法遮罩的问题
        setClippingEnabled(false);

        specialCallPresenter.getSpecialCall("");
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.query:
                String ticket = mBinding.ticketNoInput.getText().toString();
                specialCallPresenter.getSpecialCall(ticket);
                break;
            case R.id.close_tv:

            case R.id.close_iv:
                if (onDismissListener != null)
                    onDismissListener.onDismiss();
                dismiss();
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        EventBus.getDefault().post(new RemoveCallbackEvent());
        specialCallPresenter.specialCall(specialCallAdapter.getData().get(position).getTid());
    }

    @Override
    public void getSpecialCallSuccess(List<SpecialCallBean> specialCallBeans) {
        specialCallAdapter.clear();
        specialCallAdapter.addCollection(specialCallBeans);
        specialCallAdapter.notifyDataSetChanged();
    }

    @Override
    public void getSpecialCallFail(String code, String msg) {
        ToastUtil.showShort(mContext, msg);
    }

    @Override
    public void specialCallSuccess(CallBean callBean) {
        ToastUtil.showShort(mContext, R.string.specified_call_success);
        dismiss();
        if (getCallBean != null)
            getCallBean.getCallBean(callBean);
    }

    @Override
    public void specialCallFail(String code, String msg) {
        ToastUtil.showShort(mContext, msg);
    }
}
